 /**
This file is part of Studentport.

    Studentport is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Studentport is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Studentport.  If not, see <http://www.gnu.org/licenses/>.

**/
package com.groupalpha.studentport;


import com.httpclasses.perfectConnect;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 * Will present the user with the main menu.
 * presents the textviews from xml-file as a list and the selected textview will launch a new activity.
 * will load news webview with html string captured from server with perfectConnect.getNews() method.
 * @author GroupAlpha
 *
 */

public class SlistEN extends Activity {

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
    	requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.slisten);
        
        final Button logoffButton = (Button) findViewById(R.id.button2);
        final Button ladok = (Button) findViewById(R.id.ladok);
        final Button kurs = (Button) findViewById(R.id.kurs);
        final Button tenta = (Button) findViewById(R.id.tenta);
        final Button ejkurs = (Button) findViewById(R.id.ejkurs);
        final Button klara = (Button) findViewById(R.id.klara);
        final Button gasq = (Button) findViewById(R.id.gasq);
        final Button lunch = (Button) findViewById(R.id.lunch);
        final Button saldo = (Button) findViewById(R.id.saldo);
    	final WebView news = (WebView) findViewById(R.id.news);
    	final Toast toast = Toast.makeText(SlistEN.this, "You've been signed of", Toast.LENGTH_SHORT);
    	
    	ladok.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Intent i = new Intent("com.groupalpha.studentport.RESULTATEN");
                startActivity(i);
            }
        });
    	kurs.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Intent i = new Intent("com.groupalpha.studentport.KURSERDEN");
                startActivity(i);
            }
        });
    	tenta.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Intent i = new Intent("com.groupalpha.studentport.ANMTENTAEN");
                startActivity(i);
            }
        });
    	ejkurs.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
           
					Intent i = new Intent("com.groupalpha.studentport.EJAVSLKURSEN");
					startActivity(i);
			
            	
            }
        });
    	klara.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Intent i = new Intent("com.groupalpha.studentport.AVSLKURSEN");
                startActivity(i);
            }
        });
    	gasq.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Intent i = new Intent("com.groupalpha.studentport.GASQUEEN");
                startActivity(i);
            }
        });
    	lunch.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Intent i = new Intent("com.groupalpha.studentport.LUNCHCHOICEEN");
                startActivity(i);
            }
        });
    	saldo.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Intent i = new Intent("com.groupalpha.studentport.KARSALDOEN");
                startActivity(i);
            }
        });
    	
    	news.setBackgroundColor(0);
    	news.setVerticalScrollBarEnabled(false);
    	news.setWebViewClient(new WebViewClient(){
    		public boolean shouldOverrideUrlLoading(WebView view,String url){
    			if(url != null && (url.startsWith("https://")||url.startsWith("http://"))){
    				Intent i = new Intent("com.groupalpha.studentport.NEWSBROWSER").putExtra("adress", url);
    				startActivity(i);
    				
    				return true;
    			}else{
    				return false;
    			}
    			
    		}
    	});
    	try{String s = perfectConnect.getInstance().getNews();
    	news.loadData(s, "html/text", "ISO-8859-1");
    	}catch(Exception e){
    		toast.show();
    		Intent intent = new Intent(SlistEN.this, StudentportActivity.class); 
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Removes other Activities from stack 
				startActivity(intent);
    	}
    	
    	logoffButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Intent intent = new Intent(SlistEN.this, StudentportActivity.class); 
   				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Removes other Activities from stack 
   				startActivity(intent);
            }
            });
    	
    }
    
    /** Called when the menu-button on phone is clicked */
    public boolean onCreateOptionsMenu(Menu menu){
    	menu.add(0,1,0,"About").setIcon(R.drawable.ic_omoss);
    	menu.add(0,2,0,"Contact").setIcon(R.drawable.ic_kontakt);
    	return true;
    	}
    
    /** Called when user selects an option in menu */
    public boolean onOptionsItemSelected (MenuItem item){
    	switch (item.getItemId()){
    	
    	case 1:
    		Intent i1 = new Intent("com.groupalpha.studentport.OMOSSEN");
            startActivity(i1);
    	
    	return true;
    			
    	case 2:
    		Intent i2 = new Intent("com.groupalpha.studentport.KONTAKTEN");
            startActivity(i2);
    	
    	return true;
    	}
    	
    	return false;
    	}
    	@Override
    	public void onPause(){
    		super.onPause();
	
	
    	}
    	@Override
    	public void onResume(){
    		super.onResume();
	
    	}
    	
    	@Override
    	public void onBackPressed() {
    		
    		new AlertDialog.Builder(this)
    		.setTitle("Sign Out")
    		.setMessage("Are you sure you want to sign out?")
    		.setPositiveButton("Yes",
    				new DialogInterface.OnClickListener() {
    					public void onClick(DialogInterface dialog,
    							int which) {
    						
    						Intent intent = new Intent(SlistEN.this,
    								StudentportActivity.class);
    						intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Removes
    																			// other
    																			// Activities
    																			// from
    																			// stack
    						startActivity(intent);


    					}
    				})
    		.setNegativeButton("No",
    				new DialogInterface.OnClickListener() {
    					public void onClick(DialogInterface dialog,
    							int which) {
    						return;
    					}
    				}).show();

    	  
    	}



}
